package meb

import (
	"github.com/gin-gonic/gin"
	"github.com/mosongcc/gotool/gcache"
	"vitepress-serve/internal/common/hgin"
)

// OauthPolling 查询授权结果
func (ctl *Controller) OauthPolling(ctx *gin.Context) (err error) {
	args := hgin.ApiReq[OauthPollingArgs](ctx)
	body := &OauthPollingBody{Status: "0"}
	if token := gcache.Get[string](ctl.cache, args.State); token != "" {
		body.Status = "1"
		body.Token = token
		setJwtCookie(ctx, token)
	}
	hgin.ApiRes(ctx, body)
	return
}

type OauthPollingArgs struct {
	State string `json:"state"`
}

type OauthPollingBody struct {
	Status string `json:"status"` // 状态 1=正常 0=重试
	Token  string `json:"token"`  //Token
}

func setJwtCookie(ctx *gin.Context, token string) {
	hgin.SetJwtCookie(ctx, token, 60*60*24*365) // 365天
}
